﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swcommands;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.swpublished;

using SwAddinHelpers.Contracts;
using SwAddinHelpers.Attributes;
using SwAddinHelpers;
using SwAddinHelpers.Base;
using SwAddinHelpers.Sinks;

namespace SwAddinHelpers.Forms
{
    public sealed class FormsHost: UserControlHostBase
    {
        private Form _form;
        private string _caption;
        private System.Threading.Thread _formThread;
        
        private FormsHost(ISwAddinBase owner)
        {
            _OwnerAddin = owner;
        }

        public FormsHost(ISwAddinBase owner, IExternalUserControl externalControl, string Caption)
            : this(owner)
        {
            base.externalControl = externalControl.ExternalUserControl;

            _GuiControl = (UserControlBase)externalControl;
            _GuiControl.SetOwner = this;

            _caption = Caption;

            _form = new Form();
            _formThread = new System.Threading.Thread(FormThread);
            _formThread.Start();
        }

        void FormThread()
        {
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

            try
            {
                _form.FormClosing += new FormClosingEventHandler(_form_FormClosing);
                _form.Text = _caption;
                _form.MaximizeBox = false;
                _form.ShowInTaskbar = true;
                _form.SuspendLayout();
                _form.Controls.Add(_GuiControl);
                _form.Width = _GuiControl.Width + 40;
                _form.Height = _GuiControl.Height + 40;
                _form.ResumeLayout();
                Application.Run(_form);
            }
            catch (Exception ex)
            {
                _OwnerAddin.Debug.Manager.PostEvent(AddinManager.enumDebugMsgType.Exception, ex.Message);
            }
            finally
            {
                Application.ThreadException -= new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            }
        }

        void _form_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                _form.WindowState = FormWindowState.Minimized;
            }
        }

        void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            _OwnerAddin.ThrowUnhandledException(e.Exception);
        }

        public override void Teardown()
        {
            try
            {
                base.Teardown();

                _GuiControl.Untie();
                _form.Controls.Remove(_GuiControl);
                _form.FormClosing -= new FormClosingEventHandler(_form_FormClosing);
                _form.Close();
                _GuiControl = null;
                _form = null;
            }
            catch (Exception ex)
            {
                _OwnerAddin.Debug.Manager.PostEvent(AddinManager.enumDebugMsgType.Exception, ex.Message);
            }
        }

    }
}
